<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="800px"
    @close="resetForm"
  >
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="12">
          <el-form-item label="巡检人员" prop="name">
            <el-input v-model="formData.name" placeholder="巡检人员" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检时间" prop="time">
            <el-date-picker
              v-model="formData.time"
              style="width:100%"
              type="datetime"
              placeholder="巡检时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="center"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="serialno">
            <el-input v-model="formData.serialno" placeholder="设备编号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检状态" prop="status">
            <el-select v-model="formData.status" placeholder="巡检状态" clearable filterable style="width:100%">
              <el-option v-for="(item,index) in statuses" :key="index" :value="item.value" :label="item.text" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="descri">
        <el-input v-model="formData.descri" type="textarea" rows="4" placeholder="备注" clearable />
      </el-form-item>
      <el-form-item label="图片" prop="picture">
        <img-upload name="inspection" :image-url="imageUrl" @onSuccess="onSuccess" @beforeUpload="beforeUpload" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import ImgUpload from '@/components/ImgUpload'
import UploadMixin from '@/mixin/UploadMixin'
export default {
  components: {
    ImgUpload
  },
  mixins: [UploadMixin],
  data() {
    return {
      title: '新增记录',
      dialogVisible: false,
      statuses: [{ value: 1, text: '正常' }, { value: 0, text: '异常' }],
      formData: {
        name: '',
        time: '',
        serialno: '',
        status: 1,
        descri: '',
        picture: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入巡检人员姓名', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '请选择巡查时间', trigger: 'blur' }
        ],
        serialno: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择巡检状态', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    imageUrl() {
      if (this.formData.picture) {
        let prefix = ''
        if (!/http:\/\/|https:\/\//.test(this.formData.picture)) {
          prefix = `${process.env.VUE_APP_BASE_DOMAIN}`
        }
        return prefix + this.formData.picture
      }
      return ''
    }
  },
  methods: {
    onSuccess(data) {
      this.formData.picture = data.imgUrl
    },
    show(row) {
      if (row) {
        this.formData = row
      }
      this.dialogVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submitForm', this.formData)
          this.dialogVisible = false
          this.resetForm()
        } else {
          console.log(valid)
        }
      })
    },
    resetForm() {
      this.formData = {
        name: '',
        time: '',
        serialno: '',
        status: 1,
        descri: '',
        picture: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
